package org.coderjonny.io;

import org.springframework.http.HttpInputMessage;

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

public class ReReadInputStreamUtil {
    
    public static InputStream getInputStream(HttpInputMessage inputMessage) throws IOException {
        InputStream inputStream = inputMessage.getBody();
        InputStream body = null;
        if (inputStream.markSupported()) {
            inputStream.mark(1);
            body = (inputStream.read() != -1 ? inputStream : null);
            inputStream.reset();
        }
        else {
            PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
            int b = pushbackInputStream.read();
            if (b == -1) {
            }
            else {
                body = pushbackInputStream;
                pushbackInputStream.unread(b);
            }
        }
        return body;
    }
}